package com.showcai.controller.advert;

import com.github.pagehelper.PageInfo;
import com.showcai.controller.base.BaseController;
import com.showcai.def.Constant;
import com.showcai.module.Advert;
import com.showcai.module.AdvertLocation;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.AdvertService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2017-7-3.
 */
@RequestMapping("/advert")
@Controller
public class AdvertContorller extends BaseController {

    @Autowired
    AdvertService advertService;

    @RequestMapping(value = "/advertList")
    public ModelAndView advertList(Integer page, Integer pageSize) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/advert/advertList");
        Map<String, String> map = this.showRequestParameters();
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page",String.valueOf(page));
        map.put("pageSize",String.valueOf(pageSize));
        PageInfo<Advert> pageinfo = advertService.getAdvertList(map);
        mov.addObject("pageInfo", pageinfo);
        mov.addObject("bean", map);
        return mov;
    }


    @RequestMapping(value = "/advert")
    public ModelAndView certificate(Long tid) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/advert/advert");
        if(tid!=null){
            Advert advert=advertService.getAdvertById(tid);
            mov.addObject("advert",advert);
        }
        List<AdvertLocation> advertLocationList=advertService.selectAdvertLocationList(null);
        mov.addObject("advertLocationList",advertLocationList);
        return mov;
    }


    @RequestMapping(value = "/modifyAdvert")
    @ResponseBody
    public String modifyProduct(Advert advert,@RequestParam(value = "photo", required = false) MultipartFile photo) {
        ResultObj resultObj=new ResultObj();
        String filePath = "showcai/advert/";
        try {
            if(photo!=null){

                String photos = super.upLoadFile(photo.getInputStream(), filePath);
                advert.setAdvertPic(photos);
                if (StringUtils.isNotBlank(photos)) {
                    advert.setAdvertPic(photos);
                }
            }
            advertService.modifyAdvert(advert);
            resultObj.setCode(ReturnCode.SUCCESS.getCode());
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }
    @RequestMapping(value = "/stopAdvert")
    @ResponseBody
    public String stopProduct(Long tid) {
        ResultObj resultObj=new ResultObj();
        advertService.stopAdvert(tid);
        resultObj.setCode(ReturnCode.SUCCESS.getCode());
        resultObj.setMsg(ReturnCode.SUCCESS.getMsg());
        return resultObj.toJson();
    }
    @RequestMapping(value = "/operateAdvert")
    @ResponseBody
    public String operateProduct(Long tid) {
        ResultObj resultObj=new ResultObj();
        advertService.operateAdvert(tid);
        resultObj.setCode(ReturnCode.SUCCESS.getCode());
        resultObj.setMsg(ReturnCode.SUCCESS.getMsg());
        return resultObj.toJson();
    }
}
